package dice.sinanya.dice.get;

import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import dice.sinanya.entity.EntityTypeMessages;
import org.nlpcn.commons.lang.util.StringUtil;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static dice.sinanya.db.rules.Rule.selectDr;
import static dice.sinanya.db.rules.Rule.selectRule;
import static dice.sinanya.system.MessagesTag.*;
import static dice.sinanya.tools.checkdata.CheckIsNumbers.isNumeric;
import static dice.sinanya.tools.makedata.Sender.sender;

/**
 * @author SitaNya
 * 日期: 2019-08-20
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明:
 */
public class Dnd {
    private final EntityTypeMessages entityTypeMessages;
    Pattern patternDr = Pattern.compile("[ ]*([a-i])[ ]*([0-9]+)");

    public Dnd(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    /**
     * 获取煤气灯特质并发送
     */
    public void get3ry() {
        String tag = TAG_DND_3RY;
        String msg = entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(tag.substring(0, tag.length() - 2), "").trim();
        String result = selectRule("dnd", "3ry", msg);
        sender(entityTypeMessages, result);
        try {
            Thread.sleep(988);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (entityTypeMessages.getMsgGetTypes() != MsgGetTypes.privateMsg) {
            entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(entityTypeMessages.getFromQqString(), "私聊也发一份防腾讯屏蔽\n" + result);
        }
    }

    /**
     * 获取煤气灯特质并发送
     */
    public void get5ey() {
        String tag = TAG_DND_5EY;
        String msg = entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(tag.substring(0, tag.length() - 2), "").trim();
        String result = selectRule("dnd", "5ey", msg);
        sender(entityTypeMessages, result);
        try {
            Thread.sleep(988);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (entityTypeMessages.getMsgGetTypes() != MsgGetTypes.privateMsg) {
            entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(entityTypeMessages.getFromQqString(), "私聊也发一份防腾讯屏蔽\n" + result);
        }
    }

    /**
     * 获取煤气灯特质并发送
     */
    public void getPf() {
        String tag = TAG_DND_PFY;
        String msg = entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(tag.substring(0, tag.length() - 2), "").trim();
        String result = selectRule("dnd", "pf", msg);
        sender(entityTypeMessages, result);
        try {
            Thread.sleep(988);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (entityTypeMessages.getMsgGetTypes() != MsgGetTypes.privateMsg) {
            entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(entityTypeMessages.getFromQqString(), "私聊也发一份防腾讯屏蔽\n" + result);
        }
    }

    /**
     * 获取煤气灯特质并发送
     */
    public void getDr() {
        String tag = TAG_DND_DR;
        String msg = entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(tag.substring(0, tag.length() - 2), "").trim();
        Matcher matcherDr = patternDr.matcher(msg);
        String table = "";
        int times = 0;

        while (matcherDr.find()) {
            table = matcherDr.group(1);
            String timesString = matcherDr.group(2);
            if (isNumeric(timesString)) {
                times = Integer.parseInt(timesString);
            }
        }

        if ("".equals(table) || 0 == times) {
            sender(entityTypeMessages, "战利品表必须在a-i之间，且获取个数必须为非0数字\n如:.dr a 5");
            return;
        }

        ArrayList<String> result = new ArrayList<>();
        result.add("您在" + table + "表中获得的" + times + "次奖励品结果为");
        for (int i = 1; i <= times; i++) {
            result.add(selectDr(table));
        }

        sender(entityTypeMessages, StringUtil.joiner(result, "\n"));
        try {
            Thread.sleep(988);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (entityTypeMessages.getMsgGetTypes() != MsgGetTypes.privateMsg) {
            entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(entityTypeMessages.getFromQqString(), "私聊也发一份防腾讯屏蔽\n" + StringUtil.joiner(result, "\n"));
        }
    }

}
